//
//  EnergySavingFootprint.m
//  cocoapodText
//
//  Created by apple on 16/9/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "EnergySavingFootprint.h"
#import <Foundation/Foundation.h>
@interface EnergySavingFootprint ()<GYFootprintViewDelegate>

@end

@implementation EnergySavingFootprint

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
}


/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    //内容栏
    _footprintView=[[FootprintView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [_footprintView setDelegate:self];
    [self.view addSubview:_footprintView];
}

-(void)fristInitialLoad{
    APIEnergyFootManager *manager=[APIEnergyFootManager alloc];
    manager.delegate=self;
    NSString *nowdate=[CommonTool getDateTimeDifferenceWithYear:0 Month:0 Day:0 Rule:@"yyyy"];
    [manager fsgy_api_info_foot_withFalg:EFInitChart esCode:@"01" orgNo:@"520100000" year:nowdate jnjpbs:@"1" orgId:@"520100000:4: 贵阳市 :00"];
    [manager fsgy_api_info_foot_area_withFalg:EFInitTree esCode:@"01" orgNo:@"520100000" structType:@"4" orgId:@"520100000:4"];
    [_footprintView setInitCondition];
}


-(void)onEnergyFootManager:(APIEnergyFootManager *)manager result:(NSDictionary *)result Flag:(NSString *)flag{
    if ([flag isEqualToString:EFInitChart]) {
        [self onSetFootChartData:_footprintView BaseInfo:result];
    }else if([flag isEqualToString:EFInitTree]){
        //地区目录
        NSArray *lstTree=[result objectForKey:@"lstTree"];
        [_footprintView setGYMenuTableData:lstTree];
    }
}

-(void)onSetFootChartData:(FootprintView *)footprintView BaseInfo:(NSDictionary *)data{
    NSArray *legendData=[data objectForKey:@"yAxisData"];
    
    NSArray *xValData=[data objectForKey:@"data"];
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < [xValData count]; i++){
        [xVals addObject:[@(i+1) stringValue]];
    }
    NSArray *yValData=[data objectForKey:@"data"];
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < [xValData count]; i++){
        NSMutableArray *yValsTemp = [[NSMutableArray alloc] init];
        for (int j=0; j<[xValData count]; j++) {
            if (i==j) {
                NSString *valStr=[CommonTool getStringFromNSArray:yValData key:i];
                double val = [valStr doubleValue];
                double size = i;
                [yValsTemp addObject:[[BubbleChartDataEntry alloc] initWithXIndex:i value:val size:size]];
            }
        }
        BubbleChartDataSet *setTemp = [[BubbleChartDataSet alloc] initWithYVals:yValsTemp label:[CommonTool getStringFromNSArray:legendData key:i]];
        NSMutableArray *barColors = [[NSMutableArray alloc] init];
        [barColors addObject:FUIColorFromRGB(0x1f73af)];
        [barColors addObject:FUIColorFromRGB(0x59cc9a)];
        [barColors addObject:FUIColorFromRGB(0xff3a00)];
        [barColors addObject:FUIColorFromRGB(0x1fcc00)];
        [barColors addObject:FUIColorFromRGB(0x593aaf)];
        [barColors addObject:FUIColorFromRGB(0xff739a)];
        [barColors addObject:FUIColorFromRGB(0xffccaf)];
        [barColors addObject:FUIColorFromRGB(0x1f3a9a)];
        [barColors addObject:FUIColorFromRGB(0x597300)];
        [barColors addObject:FUIColorFromRGB(0x43d282)];
        [barColors addObject:FUIColorFromRGB(0x45c8dc)];
        [barColors addObject:FUIColorFromRGB(0xf77d6a)];
        [barColors addObject:FUIColorFromRGB(0x9b6ed0)];
        [barColors addObject:FUIColorFromRGB(0xf76e82)];
        [barColors addObject:FUIColorFromRGB(0x457dd0)];
        [barColors addObject:FUIColorFromRGB(0x43c86a)];
        [barColors addObject:FUIColorFromRGB(0xf76e82)];
        [barColors addObject:FUIColorFromRGB(0x9b7ddc)];
        [barColors addObject:FUIColorFromRGB(0xf7c882)];
        [barColors addObject:FUIColorFromRGB(0x1f73af)];
        [barColors addObject:FUIColorFromRGB(0x9bc8af)];
        [barColors addObject:FUIColorFromRGB(0xf773dc)];
        [barColors addObject:FUIColorFromRGB(0x1f7d82)];
        [barColors addObject:FUIColorFromRGB(0x9b7382)];
        [barColors addObject:FUIColorFromRGB(0xf77daf)];
        [barColors addObject:FUIColorFromRGB(0x1fc8dc)];
        
        [setTemp setColor:barColors[i] alpha:0.50f];
        [setTemp setDrawValuesEnabled:NO];
        
        [dataSets addObject:setTemp];
    }
    
    footprintView.xValsBubble=xVals;
    footprintView.yValsBubble=dataSets;
    footprintView.title=[CommonTool getStringFromNSDictionary:data key:@"title" defaultValue:@""];
    
    [footprintView setBubbleChartData];
}

-(void)onQueryChartData:(FootprintView *)footprintView esCode:(NSString *)esCode xslx:(NSString *)xslx selectTime:(NSString *)selectTime orgid:(NSString *)orgid area:(NSString *)area{
    APIEnergyFootManager *manager=[APIEnergyFootManager alloc];
    manager.delegate=self;
    [manager fsgy_api_info_foot_withFalg:EFInitChart esCode:esCode orgNo:@"520100000" year:selectTime jnjpbs:xslx orgId:orgid];
}

-(void)onSetData:(FootprintView *)footprintView{
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self fristInitialLoad];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
@end
